package com.xy.job.admin.common.config

import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import java.time.LocalDateTime
import java.time.ZoneOffset

object LocalDateTimeSerializer : KSerializer<LocalDateTime> {

    override val descriptor: SerialDescriptor = buildClassSerialDescriptor(LocalDateTime::class.simpleName!!)

    override fun serialize(encoder: Encoder, value: LocalDateTime) {
        val timestamp = value.toEpochSecond(ZoneOffset.UTC)
        encoder.encodeLong(timestamp)
    }

    override fun deserialize(decoder: Decoder): LocalDateTime {
        val timestamp = decoder.decodeLong()
        return LocalDateTime.ofEpochSecond(timestamp, 0, ZoneOffset.UTC)
    }
}